<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单id" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="微信小程序id" prop="appId">
        <el-input
          v-model="queryParams.appId"
          placeholder="请输入微信小程序id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="时间戳" prop="timeStamp">
        <el-input
          v-model="queryParams.timeStamp"
          placeholder="请输入时间戳"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="package_value" prop="packageValue">
        <el-input
          v-model="queryParams.packageValue"
          placeholder="请输入package_value"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付签名" prop="paySign">
        <el-input
          v-model="queryParams.paySign"
          placeholder="请输入支付签名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付时使用的小程序唯一标识" prop="openid">
        <el-input
          v-model="queryParams.openid"
          placeholder="请输入支付时使用的小程序唯一标识"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="返回状态码 0 表示成功，非 0 表示失败，此字段为通信标识，非交易标识" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择返回状态码 0 表示成功，非 0 表示失败，此字段为通信标识，非交易标识" clearable>
          <el-option
            v-for="dict in dict.type.accompany_service_status_code"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="商户订单号orderNo" prop="outTradeNo">
        <el-input
          v-model="queryParams.outTradeNo"
          placeholder="请输入商户订单号orderNo"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付交易ID-平台订单号" prop="transactionId">
        <el-input
          v-model="queryParams.transactionId"
          placeholder="请输入支付交易ID-平台订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="错误代码 10008 商户订单号已存在" prop="errCode">
        <el-input
          v-model="queryParams.errCode"
          placeholder="请输入错误代码 10008 商户订单号已存在"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="回调结果" prop="callResultCode">
        <el-input
          v-model="queryParams.callResultCode"
          placeholder="请输入回调结果"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付结果" prop="payResult">
        <el-input
          v-model="queryParams.payResult"
          placeholder="请输入支付结果"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="回调时间" prop="notifyTime">
        <el-date-picker clearable
          v-model="queryParams.notifyTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择回调时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="支付时间" prop="timeEnd">
        <el-date-picker clearable
          v-model="queryParams.timeEnd"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择支付时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="总金额" prop="totalFee">
        <el-input
          v-model="queryParams.totalFee"
          placeholder="请输入总金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="nonce_str" prop="nonceStr">
        <el-input
          v-model="queryParams.nonceStr"
          placeholder="请输入nonce_str"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="购买主题" prop="body">
        <el-input
          v-model="queryParams.body"
          placeholder="请输入购买主题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['accompany:payLog:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['accompany:payLog:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['accompany:payLog:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['accompany:payLog:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="payLogList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="payLogId" />
      <el-table-column label="订单id" align="center" prop="orderId" />
      <el-table-column label="微信小程序id" align="center" prop="appId" />
      <el-table-column label="时间戳" align="center" prop="timeStamp" />
      <el-table-column label="package_value" align="center" prop="packageValue" />
      <el-table-column label="签名类型" align="center" prop="signType" />
      <el-table-column label="支付签名" align="center" prop="paySign" />
      <el-table-column label="openid" align="center" prop="openid" />
      <el-table-column label="返回状态码" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.accompany_service_status_code" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="商户订单号" align="center" prop="outTradeNo" />
      <el-table-column label="支付交易ID" align="center" prop="transactionId" />
      <el-table-column label="错误代码" align="center" prop="errCode" />
      <el-table-column label="错误描述" align="center" prop="errMsg" />
      <el-table-column label="nonce_str" align="center" prop="nonceStr" />
      <el-table-column label="购买主题" align="center" prop="body" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="支付回调" align="center">
        <el-table-column label="回调结果" align="center" prop="callResultCode">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.accompany_service_status_code" :value="scope.row.callResultCode"/>
          </template>
        </el-table-column>
        <el-table-column label="支付结果" align="center" prop="payResult">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.accompany_service_status_code" :value="scope.row.payResult"/>
          </template>
        </el-table-column>
        <el-table-column label="货币类型" align="center" prop="feeType" />
        <el-table-column label="回调时间" align="center" prop="notifyTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.notifyTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="支付时间" align="center" prop="timeEnd" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.timeEnd, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="总金额" align="center" prop="totalFee" />
        <el-table-column label="支付类型" align="center" prop="tradeType" />
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['accompany:payLog:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['accompany:payLog:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改支付日志管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单id" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入订单id" />
        </el-form-item>
        <el-form-item label="微信小程序id" prop="appId">
          <el-input v-model="form.appId" placeholder="请输入微信小程序id" />
        </el-form-item>
        <el-form-item label="时间戳" prop="timeStamp">
          <el-input v-model="form.timeStamp" placeholder="请输入时间戳" />
        </el-form-item>
        <el-form-item label="package_value" prop="packageValue">
          <el-input v-model="form.packageValue" placeholder="请输入package_value" />
        </el-form-item>
        <el-form-item label="支付签名" prop="paySign">
          <el-input v-model="form.paySign" placeholder="请输入支付签名" />
        </el-form-item>
        <el-form-item label="支付时使用的小程序唯一标识" prop="openid">
          <el-input v-model="form.openid" placeholder="请输入支付时使用的小程序唯一标识" />
        </el-form-item>
        <el-form-item label="返回状态码 0 表示成功，非 0 表示失败，此字段为通信标识，非交易标识" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.accompany_service_status_code"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="商户订单号orderNo" prop="outTradeNo">
          <el-input v-model="form.outTradeNo" placeholder="请输入商户订单号orderNo" />
        </el-form-item>
        <el-form-item label="支付交易ID-平台订单号" prop="transactionId">
          <el-input v-model="form.transactionId" placeholder="请输入支付交易ID-平台订单号" />
        </el-form-item>
        <el-form-item label="错误代码 10008 商户订单号已存在" prop="errCode">
          <el-input v-model="form.errCode" placeholder="请输入错误代码 10008 商户订单号已存在" />
        </el-form-item>
        <el-form-item label="错误代码描述" prop="errMsg">
          <el-input v-model="form.errMsg" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="回调结果" prop="callResultCode">
          <el-input v-model="form.callResultCode" placeholder="请输入回调结果" />
        </el-form-item>
        <el-form-item label="支付结果" prop="payResult">
          <el-input v-model="form.payResult" placeholder="请输入支付结果" />
        </el-form-item>
        <el-form-item label="回调时间" prop="notifyTime">
          <el-date-picker clearable
            v-model="form.notifyTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择回调时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="支付时间" prop="timeEnd">
          <el-date-picker clearable
            v-model="form.timeEnd"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择支付时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="总金额" prop="totalFee">
          <el-input v-model="form.totalFee" placeholder="请输入总金额" />
        </el-form-item>
        <el-form-item label="nonce_str" prop="nonceStr">
          <el-input v-model="form.nonceStr" placeholder="请输入nonce_str" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="购买主题" prop="body">
          <el-input v-model="form.body" placeholder="请输入购买主题" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPayLog, getPayLog, delPayLog, addPayLog, updatePayLog } from "@/api/accompany/payLog";

export default {
  name: "PayLog",
  dicts: ['accompany_service_status_code'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 支付日志管理表格数据
      payLogList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderId: null,
        appId: null,
        timeStamp: null,
        packageValue: null,
        signType: null,
        paySign: null,
        openid: null,
        status: null,
        outTradeNo: null,
        transactionId: null,
        errCode: null,
        errMsg: null,
        callResultCode: null,
        payResult: null,
        feeType: null,
        notifyTime: null,
        timeEnd: null,
        totalFee: null,
        tradeType: null,
        nonceStr: null,
        body: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询支付日志管理列表 */
    getList() {
      this.loading = true;
      listPayLog(this.queryParams).then(response => {
        this.payLogList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        payLogId: null,
        orderId: null,
        appId: null,
        timeStamp: null,
        packageValue: null,
        signType: null,
        paySign: null,
        openid: null,
        status: null,
        outTradeNo: null,
        transactionId: null,
        errCode: null,
        errMsg: null,
        callResultCode: null,
        payResult: null,
        feeType: null,
        notifyTime: null,
        timeEnd: null,
        totalFee: null,
        tradeType: null,
        nonceStr: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        body: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.payLogId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加支付日志管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const payLogId = row.payLogId || this.ids
      getPayLog(payLogId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改支付日志管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.payLogId != null) {
            updatePayLog(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPayLog(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const payLogIds = row.payLogId || this.ids;
      this.$modal.confirm('是否确认删除支付日志管理编号为"' + payLogIds + '"的数据项？').then(function() {
        return delPayLog(payLogIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('accompany/payLog/export', {
        ...this.queryParams
      }, `payLog_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
